package csel.model;

import csel.model.entity.PlayerCharacter;

import org.apache.log4j.Logger;

final class GameData implements Observer<Object>, java.io.Serializable
{
    /**
     * Change this if we change its structure.
     */
    private static final long serialVersionUID = 6L;

    private static final Logger logger = Logger.getLogger("csel.model");
    
    private final GameGlobals gameGlobals;
    private final PlayerCharacter playerCharacter;
    private final Map map;
    
    public static GameData makeGameData(GameGlobals gameGlobals, PlayerCharacter playerCharacter, Map map)
    {
    	GameData ret;
    	
    	ret = new GameData(gameGlobals, playerCharacter, map);
    	playerCharacter.addObserver(ret);
    	
    	logger.info(String.format("Created new game (%s,%s)", playerCharacter.getTile(), playerCharacter));
    	
    	return ret;
    }
    
    private GameData(GameGlobals gg, PlayerCharacter pc, Map m)
    {
    	gameGlobals = gg;
    	playerCharacter = pc;
    	map = m;
    }
    
    public GameGlobals getGameGlobals()
    {
    	return gameGlobals;
    }
    
    public PlayerCharacter getPlayerCharacter()
    {
    	return playerCharacter;
    }
    
    public Map getMap()
    {
    	return map;
    }
    
    void startGame()
    {
    	gameGlobals.getChronoTrigger().start();
    }
    
    public void stopGame()
    {
		gameGlobals.getChronoTrigger().stop();
    }

	public void mupdate(Object o, Object param)
	{
		if (o == playerCharacter)
		{
			if (playerCharacter.getCharacterStat(CharacterStats.Lives) == 0)
			{
				/* stop game when player dies */
				stopGame();
			}
		}
	}
}
